package externalflame.systemdesign.comment.controller;

import externalflame.systemdesign.comment.dto.CommentDto;
import externalflame.systemdesign.comment.entity.Comment;
import externalflame.systemdesign.comment.exception.ResourceNotFoundException;
import externalflame.systemdesign.comment.service.CommentService;
import externalflame.systemdesign.comment.utils.ActionEnum;
import externalflame.systemdesign.comment.utils.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "评论")
@RestController
@RequestMapping("comments")
public class CommentController {

    @Resource
    CommentService commentService;

    @ApiOperation("发表评论")
    @PostMapping("/")
    public Comment addComment(@RequestHeader String userId, @RequestBody CommentDto commentDto) {
        Comment ans = commentService.addComment(userId, commentDto);
        if (ans == null) {
            throw new ResourceNotFoundException();
        }
        return ans;
    }

    @ApiOperation("删除评论")
    @DeleteMapping("/{commentId}")
    public Boolean deleteComment(@PathVariable("commentId") String commentId) {
        return commentService.deleteComment(commentId);
    }

    @ApiOperation("查看评论区的评论")
    @GetMapping("/")
    public Page<Comment> getCommentByContextId(@RequestParam String contextId,
                                               @RequestParam(required = false, defaultValue = "1") int page,
                                               @RequestParam(required = false, defaultValue = "10") int pageSize) {
        return commentService.getCommentByContextId(contextId, page, pageSize);
    }

    @ApiOperation("点赞")
    @PostMapping(value = "/{commentId}/", produces = "application/json")
    public String vote(@RequestHeader String userId, @PathVariable String commentId, @RequestParam("action") ActionEnum actionEnum) {
        return commentService.vote(userId, commentId, actionEnum);
    }

    @ApiOperation("查看评论的回复")
    @GetMapping("/{commentId}/replies")
    public Page<Comment> getReplies(@PathVariable("commentId") String commentId,
                                    @RequestParam(required = false, defaultValue = "1") int page,
                                    @RequestParam(required = false, defaultValue = "10") int pageSize) {
        return commentService.getRepliesByCommentId(commentId, page, pageSize);
    }
}
